package cn.csust.net2.manager.client.panel;

import cn.csust.net2.manager.client.ux.ServiceCallback;
import cn.csust.net2.manager.shared.po.FileNotification;
import cn.csust.net2.manager.shared.service.FileNoticServiceAsync;
import cn.csust.net2.manager.shared.util.Factory;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ComponentPlugin;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;

public class InfoFileNoticWindow extends Window{
	FileNoticServiceAsync service;
	FileNotification fileNotification;
	FormPanel panel = new FormPanel();  
	public InfoFileNoticWindow(FileNoticServiceAsync service,FileNotification fileNotification){
		this.service = service;
		this.fileNotification = fileNotification;
		init();
	}
	public void init(){
		this.setTitle("发送文件通知");
		this.setSize(500, 500);	
		panel.setHeaderVisible(false);  
		panel.setFrame(true);  
		this.setLayout(new FitLayout());
		panel.setAction(GWT.getModuleBaseURL() + "UpLoadFileNotification");
		panel.setEncoding(Encoding.MULTIPART);  
		panel.setMethod(Method.POST);  
		panel.setLayoutOnChange(true);
		panel.setButtonAlign(HorizontalAlignment.CENTER);  
		panel.setWidth(500);  
		panel.setHeight(500);  
		final FileUploadField file = new FileUploadField();
		file.setAllowBlank(false);  
		file.setName("file");  
		file.setFieldLabel("文件");  
		TextField<String> name = new TextField<String>();  
		name.setName("id");
		name.setFieldLabel("标题");  
		name.setValue(fileNotification.getFileNotificationID().toString());
		name.setVisible(false);
		panel.add(name);  
	    service.getFileAcademyHtmlInfo(fileNotification.getFileNotificationID(), new ServiceCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				
				Html html = new Html(result);
				
				panel.add(file);  
				panel.add(html,new FormData("100%"));
				
			}
		});
	    TextArea description = new TextArea();  
	    description.setPreventScrollbars(true); 
	    description.setReadOnly(true);
	    description.setValue(fileNotification.getTxtContent());
	    description.setFieldLabel("内容");  
	    
	    panel.add(description,  new FormData("100%"));  
	    LabelField lf = new LabelField();
	    lf.addPlugin(new ComponentPlugin() {
			
			@Override
			public void init(Component component) {
				 component.addListener(Events.Render, new Listener<ComponentEvent>() {  
			          public void handleEvent(ComponentEvent be) {  
			            El elem = be.getComponent().el();  
			      
			            elem.appendChild(XDOM.create("<a href = '"+GWT.getModuleBaseURL() + "download_center.do?type=F&id="+fileNotification.getFileNotificationID()+"'>下载</a>"));  
			          }  
			        }); 
				
			}
		});
	    lf.setFieldLabel("附件");
	    lf.setValue(fileNotification.getFileUrl());
	    panel.add(lf,  new FormData("100%"));  
	    DateField date1 = Factory.createDateField();
	    date1.setReadOnly(true);
	    date1.setValue(fileNotification.getEndDate());
		date1.setFieldLabel("截止日期");  
		panel.add(date1,  new FormData("40%"));  	
	    
		Button btn = new Button("Reset");  
	    btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	    	  file.reset();  
	      }  
	    });  
	    panel.addButton(btn);  
	  
	    btn = new Button("Submit");  
	    btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
	      @Override  
	      public void componentSelected(ButtonEvent ce) {  
	        if (!panel.isValid()) {  
	          return;  
	        }  
	        panel.submit();
	        MessageBox.info("Action", "You file was uploaded", null);  
	      }  
	    });  
	    panel.addButton(btn);  
		
		
		add(panel);  
		this.show();
	}
}
